package com.example.statistics.ui.port

import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.bean.ExchangeRate
import com.example.statistics.common.BaseLoadingListActivity
import com.example.statistics.ex.*
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityForResult
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.holder.ViewInjector
import org.jetbrains.anko.toast
import java.util.Calendar
import java.util.Locale

class ExchangeRateListActivity : BaseLoadingListActivity<ExchangeRate>() {

    override val title: String
        get() = "海关汇率"
    override val url: String
        get() = UrlConfig.PURCHASE_RATE_LIST
    override val itemLayoutRes: Int
        get() = R.layout.list_item_exchange_rate

    override fun itemViewConvert(adapter: SlimAdapter, injector: ViewInjector, bean: ExchangeRate, position: Int) {
        injector.text(R.id.tv_date, bean.date)
            .text(R.id.tv_dollar, bean.dollar.toString())
            .text(R.id.tv_euro, bean.euro.toString())
            .onActionLongClicked(
                "删除" to {
                    httpGet<Any>(UrlConfig.PURCHASE_RATE_DELETE, config = {
                        listParams = listOf("id" to bean.id)
                    }) {
                        refresh()
                    }
                }
            )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        topBarRightImage {
            var date = ""
            if (dataList.isNotEmpty()) {
                val lastDate = dataList.first().date.parse("yyyy-MM")
                val calendar = Calendar.getInstance(Locale.getDefault())
                if (lastDate != null) {
                    calendar.time = lastDate
                    calendar.add(Calendar.MONTH, 1)
                    date = calendar.time.format("yyyy-MM").orEmpty()
                    activityForResult<ExchangeRateEditActivity>(Constants.DATE to date) {
                        refresh()
                    }
                } else {
                    toast("日期错误")
                }
            } else {
                toast("日期错误")
            }

        }
    }


}